package com.lovo.ssc.controller;


import com.lovo.dto.PendingCommodityDto;
import com.lovo.ssc.bean.Commoditydescribe;
import com.lovo.ssc.bean.Commodityimg;
import com.lovo.ssc.bean.Pendingcommodity;
import com.lovo.ssc.dto.ChartDto;
import com.lovo.ssc.dto.Dto1;
import com.lovo.ssc.service.ICommoditydescribeService;
import com.lovo.ssc.service.ICommodityimgService;
import com.lovo.ssc.service.IPendingcommodityService;
import com.lovo.sscsys.remoteService.IsscsysRemoteService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/pendingcommodity")
@Api(tags = "待上架商品应用控制器005--------刘文龙")
@Slf4j
public class PendingcommodityController {


	@Autowired
	public RabbitTemplate rabbitTemplate;

	@Autowired
	IPendingcommodityService pendingcommodityService;
	@Autowired
	ICommodityimgService imgService;
	@Autowired
	ICommoditydescribeService describeService;

	@Autowired
	IsscsysRemoteService isscsysRemoteService;

	@ApiOperation(value = "获取待上架商品信息")
	@GetMapping("find")
	public void test() {
		log.info("-----------获取待上架商品信息-----------------");
		List<PendingCommodityDto> pendingCommodityDtos = isscsysRemoteService.findByOriginal();
		for (PendingCommodityDto p :
				pendingCommodityDtos) {
			Pendingcommodity pendingcommodity = new Pendingcommodity();

			pendingcommodity.setCommoditycode(p.getCommodityCode());
			pendingcommodity.setCommodityname(p.getCommodityname());
			pendingcommodity.setNumber(p.getNumber());
			pendingcommodity.setType(p.getType());
			pendingcommodity.setNorms(p.getNorms());
			pendingcommodity.setPrice(Double.valueOf(String.valueOf(p.getPrice())));

			pendingcommodityService.save(pendingcommodity);
		}
	}


	@ApiOperation("根据日期查询销售数据")
	@GetMapping("/findChartData")
	public List<ChartDto> findChartData(@RequestParam("type") String type,
	                                    @RequestParam("startDate") String startDate,
	                                    @RequestParam("endDate") String endDate) {
		return pendingcommodityService.findChartData(type,startDate, endDate);
	}

	@ApiOperation(value = "从本地获取待上架商品")
	@GetMapping("getPendingcommodity")
	public List<Pendingcommodity> getPendingcommodity() {
		return pendingcommodityService.list();
	}


	@ApiOperation(value = "编辑待上架商品")
	@PostMapping("/sendPendingCommodityToBackstage")
//	@ApiImplicitParams(value = {
//			@ApiImplicitParam(name = "commodityCode", value = "商品编号"),
//			@ApiImplicitParam(name = "imgPathList", value = "商品图片路径集合", dataType = "List", allowMultiple = true),
//			@ApiImplicitParam(name = "describe", value = "商品描述"),
//			@ApiImplicitParam(name = "price", value = "价格")
//	})
	public void sendPendingCommodityToBackstage(Dto1 dto1) {
		System.out.println(dto1 + "-----------------------------");
		//添加商品图片
		for (String imgPath : dto1.getImgPathList()) {
			Commodityimg img = new Commodityimg();
			img.setFkcommoditycode(dto1.getCommodityCode());
			img.setImgpath(imgPath);
			imgService.save(img);
		}

		//添加商品描述
		Commoditydescribe commoditydescribe = new Commoditydescribe();
		commoditydescribe.setCommoditycode(dto1.getCommodityCode());
		commoditydescribe.setCommoditydescribe(dto1.getDescribe());
		describeService.save(commoditydescribe);

		//向后台系统发送编辑后的待上架商品信息
		//根据商品编号得到待上架商品信息
		Pendingcommodity p = pendingcommodityService.findByCommodityCode(dto1.getCommodityCode());
		//将价格改为销售价格
		log.info(dto1.getPrice().toString());
		p.setPrice(dto1.getPrice());
		//将p转化为PendingCommodityDto
		PendingCommodityDto pendingcommodityDto = new PendingCommodityDto();
		pendingcommodityDto.setCommodityCode(p.getCommoditycode());
		pendingcommodityDto.setCommodityname(p.getCommodityname());
		pendingcommodityDto.setNorms(p.getNorms());
		pendingcommodityDto.setType(p.getType());
		pendingcommodityDto.setNumber(p.getNumber());
		pendingcommodityDto.setPrice(Float.valueOf(String.valueOf(p.getPrice())));
		//通过MQ，将pendingcommodityDto发送给商城后台系统
		rabbitTemplate.convertAndSend(MQUtil.pendingCommodityFanoutExchange, MQUtil.pendingCommodityQueue, pendingcommodityDto);
		//根据商品编号删除对应的待上架商品
		pendingcommodityService.deletePendingCommodityByCode(dto1.getCommodityCode());

	}


}
